package controller;

import java.io.IOException;
import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EmployeeDTO;
import model.LeaveDTO;
import bizlogic.Employee;
import bizlogic.LeaveManager;

/**
 * Servlet implementation class LeaveHistoryServlet
 */
@WebServlet("/LeaveHistoryServlet")
public class LeaveHistoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LeaveHistoryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws NumberFormatException {

		Employee emp = new Employee();
		LeaveManager lm = new LeaveManager();// bizlogic
		LeaveDTO ldto = new LeaveDTO();

	//	EmployeeDTO empsession= new EmployeeDTO();
	//	HttpSession session=request.getSession();
	//	empsession = (EmployeeDTO)session.getAttribute("Admins");

		String status = "applied";
		request.getParameter("eid");
		System.out.println("EID"+request.getParameter("eid"));
		
	    //int eid = Integer.parseInt(request.getParameter("eid"));
		 
		// String empid=request.getParameter("employeeID");
		// int id= Integer.parseInt(request.getParameter("employeeID"));

		//int managerid=empsession.getManagerID();
		//System.out.println("managerid="+managerid);
		int managerid = 5;
		int leaveid = 4;
		String app = (String) request.getParameter("app");
		
		if(app!=null)
		{
		Logger.getLogger(getClass().getName()).log(Level.INFO,
				"Approve Flag: " + app);
		if (app.equalsIgnoreCase("true")) {
			ldto.setLeaveID(leaveid);
			ldto.setStatus("approved");
			lm.updateLeaveStatus(ldto);
			System.out.println("servlet ====" + ldto.toString());
		} else if (app.equalsIgnoreCase("false")) {
			ldto.setLeaveID(leaveid);
			ldto.setStatus("rejected");
			lm.updateLeaveStatus(ldto);
			ArrayList<EmployeeDTO> data = emp.findEmployeeByManager(managerid,status);
			request.setAttribute("employees", data);
		}
			RequestDispatcher rd = request.getRequestDispatcher("/LeaveHistory.jsp");
			try {
				rd.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		else if(app==null)
		{
		ArrayList<EmployeeDTO> data = emp.findEmployeeByManager(managerid,status);
		request.setAttribute("employees", data);
		RequestDispatcher rd = request.getRequestDispatcher("/LeaveHistory.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}
